// NeLNS - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
// Copyright (C) 2010  Winch Gate Property Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H

#ifndef NELNS_CONFIG
#define NELNS_CONFIG ""
#endif // NELNS_CONFIG

#ifndef NELNS_LOGS
#define NELNS_LOGS ""
#endif // NELNS_LOGS

#include "nel/misc/types_nl.h"

#include <cstdio>
#include <ctype.h>
#include <cmath>

#include <vector>
#include <map>

#include "nel/misc/debug.h"
#include "nel/misc/config_file.h"
#include "nel/misc/displayer.h"
#include "nel/misc/command.h"
#include "nel/misc/log.h"
#include "nel/misc/window_displayer.h"
#include "nel/misc/path.h"
#include "nel/net/service.h"
#include <server_share/server_def.h>
#include <server_share/msg_struct_zone.h>
#include <server_share/lua_engine.h>

#include "process_msg_zs.h"
#include "scene_factory.h"
#include "mission_mgr.h"


using namespace NLMISC;
using namespace NLNET;
using namespace std;
using namespace ZS;

CFileDisplayer *Fd = NULL;
CStdDisplayer Sd;

//
// Functions
//

void displayInfo ()
{
	ICommand::execute ("info", *NLMISC::InfoLog);
}

// a new entities game service connecting to me, add it
void cbEGSConnection (const std::string &serviceName, NLNET::TServiceId  sid, void *arg)
{
	MSG_STR::CZoneService& zone = SceneFactory.GetSceneConfig();
	NLNET::CMessage msgout("ZS_STATE");
	msgout.serial(zone);
	NLNET::CUnifiedNetwork::getInstance()->send (sid, msgout);
}

NLNET::TUnifiedCallbackItem ZSCallbackArray[] =
{
	{  "PDC",                  cbPlayerDisConnect },		//	from EGS
	{  "LOGOUT",               cbLogoutPlayer },			//	from EGS	replace account 
	{  "LOGOUT_OK",            cbLogoutPlayerOK },		//	from LS
	{  "GET_PLAYER_SUCESS",    cbInitPlayer },			//	from EGS    player login
	


};

class CZoneService : public NLNET::IService
{
public:

	CZoneService () { }

	/// Init the service, load the universal time.
	void init ()
	{
		string fn = IService::getInstance()->SaveFilesDirectory;
		string log_file = fn + ConfigFile.getVar("LogDir").asString();

		string zone_log = log_file + "zone_service.stat";
		nlinfo("Zone stat in directory '%s'", zone_log.c_str());
		Fd = new NLMISC::CFileDisplayer(zone_log);
		getGlobal_Loger().addDisplayer(Fd);
		if (WindowDisplayer) getGlobal_Loger().addDisplayer (WindowDisplayer);

		string lua_log = log_file + "lua_engine.log";
		LuaEngine.Init(lua_log);
		MissionMgr.init();
		SceneFactory.init();
		

		CUnifiedNetwork::getInstance()->setServiceUpCallback("EGS", cbEGSConnection, NULL);
		getGlobal_Loger().displayNL ("Zone Service initialized");
	}

	bool update ()
	{
		return true;
	}

	/// release the service, save the universal time
	void release ()
	{
		getGlobal_Loger().displayNL ("Zone Service released");
	}
};

// Service instantiation
NLNET_SERVICE_MAIN (CZoneService, "ZS", "zone_service", 0, ZSCallbackArray, NELNS_CONFIG, NELNS_LOGS);

//
// Commands
//

NLMISC_COMMAND (info, "service information.", "")
{
	if(args.size() != 0) return false;
	log.displayNL ("service information.");
	return true;
}
